//
//  AppDelegate.swift
//  swiftweibo
//
//  Created by ffxk007 on 2017/6/1.
//  Copyright © 2017年 ffxk007. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    
    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        setupUIAdditions()
        
        
//        sleep(2)
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        // Override point for customization after application launch.
        loadAppInfo()
        return true
    }
}

// MARK: - 设置应用额外信息
extension AppDelegate {
    fileprivate func setupUIAdditions(){
        //设置svphd最小解除时间
        SVProgressHUD.setMaximumDismissTimeInterval(1)
        //设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        //设置授权显示通知
        //#available 检测设备版本，如果是10.0以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]) { (success, error) in
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            // Fallback on earlier versions
            //取得用户授权显示通知[上方的提示条/声音/badgenumber]
            let notifySetting  = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            
            UIApplication.shared.registerUserNotificationSettings(notifySetting)
            
        }

    }
}

extension AppDelegate {
    fileprivate func loadAppInfo(){
        DispatchQueue.global().async {
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            let data = NSData(contentsOf:url!)
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            data?.write(toFile: jsonPath, atomically: true)
            print("应用加载程序完毕 \(jsonPath)")
            
        }
    }
}
